package org.zoomdev.zoom.validator.impl;

import org.zoomdev.zoom.validator.Validator;
import org.zoomdev.zoom.validator.annotations.Length;
import org.zoomdev.zoom.validator.annotations.Size;

public class LengthValidator implements Validator {
    private final int min;
    private final int max;

    private final String message;

    public LengthValidator(Length length) {
        this.min = length.min();
        this.max = length.max();
        this.message = length.message();
    }

    public LengthValidator(Size length) {
        this.min = length.min();
        this.max = length.max();
        this.message = length.message();
    }

    @Override
    public boolean isValid(Object value) {
        if (!(value instanceof CharSequence)) return true;
        int length = ((CharSequence) value).length();
        return length >= min && length <= max;
    }

    @Override
    public String getMessage() {
        return message;
    }
}